package cn.gs.dao;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import cn.gs.domain.Customer;
import cn.gs.domain.LinkMan;
import cn.gs.utils.HibernateUtil;


@Component(value = "selectLinkManDao")
@Scope("prototype")
public class SelectLinkManDaoIpml implements SelectLinkManDao {

	Session session = HibernateUtil.openSession();
	Transaction transaction = session.getTransaction();
	
	public List<LinkMan> selectLinkMan(String lkm_name, String cust_name, int page) {
		
		List<LinkMan> lm=new ArrayList<LinkMan>();
		
		boolean lkmname;
		boolean custname;
		if (lkm_name==null||lkm_name.isEmpty()) {
			lkmname = true;
		} else {
			lkmname = false;
		}
		if (cust_name==null||cust_name.isEmpty()) {
			custname = true;
		} else {
			custname = false;
		}
		if (lkmname && custname) {
			transaction.begin();
			Query query = session.createQuery("from LinkMan");
			int i = (page - 1) * 3;
			query.setFirstResult(i);
			query.setMaxResults(3);
			List<LinkMan> list = query.list();
			transaction.commit();
			session.close();
			return list;
		}	
		if((!lkmname)&& custname) {
			transaction.begin();
			Query query = session.createQuery("from LinkMan where lkm_name like ?");
			query.setParameter(0, "%" + lkm_name + "%");
			int i = (page - 1) * 3;
			query.setFirstResult(i);
			query.setMaxResults(3);
			List list = query.list();
			transaction.commit();
			session.close();
			return list;
		}
		
		if(lkmname&& (!custname)) {
			transaction.begin();
			Query query = session.createQuery("select cust_id from Customer where cust_name like ?");
			query.setParameter(0, "%" + cust_name + "%");
			List<Long> list = query.list();
			Iterator<Long>  it=list.iterator();
			while(it.hasNext()) {
				long a=it.next();
				Query query1 = session.createQuery("from LinkMan where lkm_cust_id like ?");
				query1.setParameter(0,  a);	
				int i = (page - 1) * 3;
				query1.setFirstResult(i);
				query1.setMaxResults(3);
				List<LinkMan> list1=query1.list();
				System.out.println("核心：找出的联系人："+list1);
				for(LinkMan  linkman:list1 ) {					
					lm.add(linkman);			
				}	
			}
			transaction.commit();
			session.close();
			int i = (page - 1) * 3;//  1 2 3 4 5
			List<LinkMan> fn=new ArrayList<LinkMan>();
			for(int i1=i;i1<lm.size();i1++) {
				if(i1<i+3) {
				fn.add(lm.get(i1)) ;
				}
			}
			return fn;		
		}
		if((!lkmname)&& (!custname)) {
			transaction.begin();
			Query query = session.createQuery("select cust_id from Customer where cust_name like ?");
			query.setParameter(0, "%" + cust_name + "%");
			List<Long> list = query.list();
			Iterator<Long>  it=list.iterator();
			while(it.hasNext()) {
				long a=it.next();
				Query query1 = session.createQuery("from LinkMan where lkm_cust_id like ? and lkm_name like ? ");
				query1.setParameter(0,  a);
				query1.setParameter(1, "%"+lkm_name+"%");
				System.out.println("联系人姓名"+lkm_name);
				List<LinkMan> list1=query1.list();
				System.out.println("打印添加的LIST"+list1);
				for(LinkMan  linkman:list1 ) {					
					lm.add(linkman);			
				}	
			}
			transaction.commit();
			session.close();
			int i = (page - 1) * 3;//  1 2 3 4 5
			List<LinkMan> fn=new ArrayList<LinkMan>();
			for(int i1=i;i1<lm.size();i1++) {
				if(i1<i+3) {
					System.out.println("添加了"+i1+"次");
				fn.add(lm.get(i1)) ;
				}
			}
			return fn;		
		}
		
		return null;		
	}

	
	
	
	
	@Override
	public List<LinkMan> selectPageCount(String lkm_name, String cust_name) {
		List<LinkMan> lm=new ArrayList<LinkMan>();
		boolean lkmname;
		boolean custname;
		if (lkm_name==null||lkm_name.isEmpty()) {
			lkmname = true;
		} else {
			lkmname = false;
		}
		if (cust_name==null||cust_name.isEmpty()) {
			custname = true;
		} else {
			custname = false;
		}
		if (lkmname &&custname) {
			transaction.begin();
			Query query = session.createQuery("select count(l) from LinkMan l");
			List list = query.list();
			transaction.commit();
			session.close();
			return list;
		}
		
		if ((!lkmname) &&custname) {
			transaction.begin();
			Query query = session.createQuery("select count(l) from LinkMan l   where lkm_name like ?");
			query.setParameter(0, "%" + lkm_name + "%");
			List list = query.list();	
			transaction.commit();
			session.close();
			return list;
		}
		if (lkmname &&(!custname)) {
			transaction.begin();
			Query query = session.createQuery("select cust_id from Customer where cust_name like ?");
			query.setParameter(0, "%" + cust_name + "%");
			List<Long> list = query.list();
			Iterator<Long>  it=list.iterator();
			while(it.hasNext()) {
				long a=it.next();
				Query query1 = session.createQuery("from LinkMan where lkm_cust_id like ?");
				query1.setParameter(0,  a);	
				List<LinkMan> list1=query1.list();
				for(LinkMan  linkman:list1 ) {					
					lm.add(linkman);			
				}	
			}
			transaction.commit();
			session.close();
			List l=new ArrayList();
			l.add(lm.size());		
			return l;
		}
		if((!lkmname)&& (!custname)) {
			transaction.begin();
			Query query = session.createQuery("select cust_id from Customer where cust_name like ?");
			query.setParameter(0, "%" + cust_name + "%");
			List<Long> list = query.list();
			Iterator<Long>  it=list.iterator();
			while(it.hasNext()) {
				long a=it.next();
				Query query1 = session.createQuery("from LinkMan where lkm_cust_id like ? and lkm_name like ? ");
				query1.setParameter(0,  a);
				query1.setParameter(1, "%"+lkm_name+"%");
				System.out.println("联系人姓名"+lkm_name);
				List<LinkMan> list1=query1.list();
				System.out.println("打印添加的LIST"+list1);
				for(LinkMan  linkman:list1 ) {					
					lm.add(linkman);			
				}	
			}
			transaction.commit();
			session.close();;
			List l=new ArrayList();
			l.add(lm.size());		
			return l;	
		}
		
		
		
		
		
		return null;
	}

}
